#---
# title: "CSC 171 - Lab 5"
# author: "Student Name"
# output: html_document
#---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

### Instructions

In this lab, you will begin to get oriented with R and work with some data. 

#### How to **complete** this assignment.

* Attempt each exercise in order.

* In each code chunk, if you see "<font color="789922"># INSERT CODE HERE"</font>, then you are expected to add some code to create the intended output (Make sure to erase "# INSERT CODE HERE" and place your code in its place).

* If my instructions say to "Run the code below..." then you do not need to add any code to the chunk. 

* Many exercises may require you to type some text below the code chunk, interpreting the output and answering the questions. 

* Please follow the Davidson Honor Code and rules from the course syllabus regarding seeking help with this assignment.

#### How to **submit** this assignment.

* When you are finished, click the "Knit" button at the top of this panel. If there are no errors, an word file should pop up after a few seconds.

* Take a look at the resulting word file that pops up. Make sure everything looks correct, your name is listed at the top, and that there is no 'junk' code or output. 

* Save the word file (to your local computer, and/or to a cloud location) as: **Lab 5 "Insert Your Name"**.

* Use [this link](https://forms.gle/6TwuoqAfbHyiLQ2s9) to upload your word file to my Google Drive folder. **Do not** upload the original .Rmd version. 

* This assignment is <font color="darkred">**due Thursday, July 14, 2022, no later than 9:30 am Eastern**</font>. Points will be deducted for late submissions. 

* <font color="darkblue">TIP:</font> Start early so that you can troubleshoot any issues with knitting to word. 

#### Grading Rubric

There are 6 possible points on this assignment.

**Baseline (C level work)**

- Your .Rmd file knits to word without errors.
- You answer questions correctly but do not use complete sentences.
- There are typos and 'junk code' throughout the document.
- You do not put much thought or effort into the Reflection answers.

**Average (B level work)**

- You use complete sentences to answer questions. 
- You attempt every exercise/question. 

**Advanced (A level work)**

- Your code is simple and concise.
- Unnecessary messages from R are hidden from being displayed in the word.
- Your document is typo-free.
- At the discretion of the instructor, you give exceptionally thoughtful or insightful responses.

---


#### **Exercise 1. (3 points)** 

In this exercise, we will generate simulated data, and will then use this data to perform best subset selection.

(A) Use the `rnorm()` function to generate a predictor X of length n = 100, as well as a noise vector $\epsilon$ of length n = 100 (Hint: remember to set seed!).

(B) Generate a response vector Y of length n = 100 according to the model $Y = B_0 + B_1X + B_2X^2 + B_3X^3 + \epsilon$

  - Where $B_0, B_1, B_2, B_3$ are the constants 2, 3, -1, 0.5, respectively. 
  
(C) Use the `regsubsets()` function (within the `leaps` package) to perform best subset selection in order to choose the best model containing the predictors $X,X^2,...,X^{10}$. What is the best model obtained according to $C_p$, BIC, and adjusted $R^2$? Show some plots to provide evidence for your answer, and report the coefficients of the best model obtained (Note: you might need to use the `data.frame()` function to create a single data set containing both X and Y. Additionally, you need to assign your `regsubsets()` model summary to view the variables of interest).

(D) Repeat (C), using forward stepwise selection and also using backwards stepwise selection. How does your answer compare to the results in (C)

(E) Now fit a lasso model to the simulated data, again using $X,X^2, . . . , X^{10}$ as predictors. Use cross-validation to select the optimal value of $\lambda$. Create plots of the cross-validation error as a function of $\lambda$. Report the resulting coefficient estimates, and discuss the results obtained.

```{r}
#insert code here
```

<font color="darkred">**ANSWER:**</font>






#### **Exercise 2. (3 points)** 

In this exercise, we will predict the number of applications received using the other variables in the `College` data set.

(A) Split the data set into a training set and a test set.

(B) Fit a linear model using least squares on the training set, and report the test error obtained.

(C) Fit a ridge regression model on the training set, with $\lambda$ chosen by cross-validation. Report the test error obtained.

(D) Fit a lasso model on the training set, with $\lambda$ chosen by cross-validation. Report the test error obtained, along with the number of non-zero coefficient estimates.

(E) Fit a PCR model on the training set, with $M$ chosen by cross-validation. Report the test error obtained, along with the value of $M$ selected by cross-validation.

(F) Fit a PLS model on the training set, with $M$ chosen by cross-validation. Report the test error obtained, along with the value of $M$ selected by cross-validation.

(G) Comment on the results obtained. How accurately can we pre-dict the number of college applications received? Is there much difference among the test errors resulting from these five approaches?

```{r}
#insert code here
```

<font color="darkred">**ANSWER:**</font>